﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Studio77.RuneMaster.Core;
using Studio77.RuneMaster.Utils;
using Microsoft.Phone.Tasks;
using System.Windows.Threading;

namespace Studio77.RuneMaster
{
    public partial class MainPage : PhoneApplicationPage
    {
        private static bool isShowOpenAd = false;

        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            this.btnNewUser.Click += btnNewUser_Click;
            this.btnCloseNewUser.Click += btnCloseNewUser_Click;
            this.btnGetCoin.Click += btnGetCoin_Click;
        }

        /// <summary>
        /// 是否已经打开对话框
        /// </summary>
        bool isOnDialog = false;

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (isOnDialog)
            {
                StoryboardHideNewUser.Begin();
                isOnDialog = false;
                e.Cancel = true;
            }
            base.OnBackKeyPress(e);

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //清除页面堆栈
            try
            {
                for (int i = 0; i < 4; i++)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            catch
            { }

            //设置继续按钮可用性
            if (StageManager.Instance.SavedGameRecord != null)
            {
                this.btnContinue.IsEnabled = true;
                this.txtLastRecord.Visibility = System.Windows.Visibility.Visible;
                this.txtLastRecord.Text = GetRecordStageName();
            }
            else
            {
                this.btnContinue.IsEnabled = false;
                this.txtLastRecord.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (StageManager.Instance.IsPlayBackground)
            {
                this.btnMusicOn.Visibility = System.Windows.Visibility.Visible;
                this.btnMusicOff.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.btnMusicOn.Visibility = System.Windows.Visibility.Collapsed;
                this.btnMusicOff.Visibility = System.Windows.Visibility.Visible;
            }

            if (StageManager.Instance.IsPlaySound)
            {
                this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
                this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
                this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
            }

            this.btnNewUser.Visibility = System.Windows.Visibility.Collapsed;
            if (StageManager.Instance.IsDoNewUserTask)
            {
                this.btnNewUser.Visibility = System.Windows.Visibility.Collapsed;
            }
            else if (UmengSDK.UmengAnalytics.GetOnlineParam("useGift") == "1")
            {
                this.btnNewUser.Visibility = System.Windows.Visibility.Visible;
            }

            //播放背景音乐
            AudioPlayHelper.PlayBackground("background.mp3");

            String usemokaStr = UmengSDK.UmengAnalytics.GetOnlineParam("usemoka");
            //0-关闭 1-同时开启 2-开启banner 3-开启开屏
            if ((usemokaStr == "1" || usemokaStr == "3") && !isShowOpenAd)
            {
                isShowOpenAd = true;
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(1);
                timer.Tick += (object ss, EventArgs ee) =>
                {
                    timer.Stop();
                    string dateStr = DateTime.Now.ToString("yyyyMMdd") + (DateTime.Now.Hour < 12 ? "A" : "P");
                    if (StageManager.Instance.LastFullScreenDate != dateStr)
                    {
                        StageManager.Instance.LastFullScreenDate = dateStr;
                        NavigationService.Navigate(new Uri("/Game77SharedLib;component/MokaPage.xaml?channel=jkfwds", UriKind.RelativeOrAbsolute));
                    }
                };
                timer.Start();

            }
        }

        private string GetRecordStageName()
        {
            string result = string.Empty;

            switch (StageManager.Instance.SavedGameRecord.GameMode)
            {
                case "e":
                    result += "EASY ";
                    break;
                case "n":
                    result += "NORMAL ";
                    break;
                case "h":
                    result += "HARD ";
                    break;
            }

            result += "STAGE " + StageManager.Instance.SavedGameRecord.StageIndex + "-" + StageManager.Instance.SavedGameRecord.SubStageIndex;

            return result;
        }

        private void btnContinue_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);

            // 在此处添加事件处理程序实现。
            if (StageManager.Instance.SavedGameRecord != null)
            {
                StageManager.Instance.CurrentGameMode = StageManager.Instance.SavedGameRecord.GameMode;
                StageManager.Instance.CurrentRestartChange = StageManager.Instance.SavedGameRecord.LeftLifeCount;
                StageManager.Instance.CurrentStageIndex = StageManager.Instance.SavedGameRecord.StageIndex;
                StageManager.Instance.CurrentSubStageIndex = StageManager.Instance.SavedGameRecord.SubStageIndex;
                this.NavigationService.Navigate(new Uri("/Pages/GamePage.xaml?isNewGame=0", UriKind.RelativeOrAbsolute));
            }

        }

        private void btnNewGame_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            this.NavigationService.Navigate(new Uri("/Pages/ModeSelectPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void btnGameRecord_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            this.NavigationService.Navigate(new Uri("/Pages/ScorePage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void btnSoundOn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //关闭音效
            StageManager.Instance.IsPlaySound = false;
            this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
            this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
        }

        private void btnMusicOn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //关闭音乐
            StageManager.Instance.IsPlayBackground = false;
            AudioPlayHelper.StopBackground();
            this.btnMusicOn.Visibility = System.Windows.Visibility.Collapsed;
            this.btnMusicOff.Visibility = System.Windows.Visibility.Visible;
        }

        private void btnSoundOff_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //打开音效
            StageManager.Instance.IsPlaySound = true;
            this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
            this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void btnMusicOff_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //打开音乐
            StageManager.Instance.IsPlayBackground = true;
            AudioPlayHelper.PlayBackground("background.mp3");
            this.btnMusicOn.Visibility = System.Windows.Visibility.Visible;
            this.btnMusicOff.Visibility = System.Windows.Visibility.Collapsed;
        }

        void btnGetCoin_Click(object sender, RoutedEventArgs e)
        {
            AudioPlayHelper.PlayBackground("background.mp3");
            MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
            marketplaceReviewTask.Show();
            StageManager.Instance.IsCommented = true;
            StageManager.Instance.IsDoNewUserTask = true;

            //增加5万金币
            StageManager.Instance.TotalG += 50000;

            this.btnGetCoin.Content = "已领取";
            this.btnGetCoin.IsEnabled = false;
            this.btnNewUser.Visibility = System.Windows.Visibility.Collapsed;
        }

        void btnCloseNewUser_Click(object sender, RoutedEventArgs e)
        {
            AudioPlayHelper.PlayBackground("background.mp3");
            isOnDialog = false;
            this.StoryboardHideNewUser.Begin();
        }

        void btnNewUser_Click(object sender, RoutedEventArgs e)
        {
            AudioPlayHelper.PlayBackground("background.mp3");
            isOnDialog = true;
            this.StoryboardShowNewUser.Begin();
        }
    }
}